package com.aplink.generic.util.validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidExpression extends ValidationBase {

    private String valExpression;

    public ValidExpression() {
        this.ErrorMessage = "";
        this.setValExpression( "" );
    }

    public ValidExpression( String valexpression, String errorMessage ) {
        this.valExpression = valexpression;
        this.ErrorMessage = errorMessage;
    }

    @Override
    public boolean checkValidation( Object attValue ) {
        // if there is no expression we will always return true
        if ( this.valExpression.compareTo( "" ) == 0 ) {
            return true;
        }
        final String testData = attValue.toString();

        // use the regex defined in config files
        // String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +
        // "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        final Pattern pattern = Pattern.compile( this.valExpression );
        final Matcher matcher = pattern.matcher( testData );

        return matcher.matches();
        // here you can use both of Android pattern
        // return
        // android.util.Patterns.EMAIL_ADDRESS.matcher(testData).matches();

    }

    public String getValExpression() {
        return this.valExpression;
    }

    public void setValExpression( String valExpression ) {
        this.valExpression = valExpression;
    }

}
